﻿using Qy.Core;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Qy.Scenario;

[AppService]
public class MenuService : QyRepository<Menu>
{

    public MenuService(ApplicationContext applicationContext) : base(applicationContext)
    {
    }

    public async Task<List<Menu>> GetAllAsync()
    {
        List<Menu> cate = await GetAllListAsync("Sort asc");
        return cate;
    }

    public async Task<Menu> GetInfo(int cid)
    {
        List<Menu> cates = await GetAllAsync();
        return cates.FirstOrDefault(a => a.Id == cid);
    }

    /// <summary>
    /// 根据权限获取列表
    /// </summary>
    /// <returns></returns>
    public async Task<List<Menu>> CateRoleListAsync(IEnumerable<int> cids, int type = 0)
    {
        List<Menu> cates = await GetAllAsync();
        if (cids == null) return cates.Where(o => o.Type == type).ToList();
        List<Menu> newCates = new();
        foreach (int cid in cids)
        {
            Menu cate = new();
            if (type == 0) cate = cates.FirstOrDefault(o => o.Id == cid);
            else cate = cates.FirstOrDefault(o => o.Type == type && o.Id == cid);
            if (cate != null) newCates.Add(cate);
        }
        return newCates;
    }
    /// <summary>
    /// 树形列表
    /// </summary>
    /// <param name="adminUser"></param>
    /// <returns></returns>
    public async Task<IEnumerable<ViewMenu>> GetCatTree()
    {
        List<Menu> cates = await GetAllAsync();
        List<ViewMenu> treeModels = new();
        MenuExtensions.IndentedCate(cates, treeModels);
        return treeModels;
    }

    /// <summary>
    /// 转换带层级符号树形
    /// </summary>
    /// <param name="adminUser"></param>
    /// <param name="role"></param>
    /// <returns></returns>
    public async Task<IEnumerable<ViewMenu>> GetCateListAsync(int type)
    {
        List<Menu> all = await GetAllAsync();
        List<Menu> cates = all.Where(o => o.Type == type).ToList();
        return cates.AsCateListItems();//导出带 封装数据
    }

    /// <summary>
    /// 管理列表
    /// </summary>
    /// <returns></returns>
    public async Task<List<ViewMenu>> GetCategorys()
    {
        return await GetCates(0, await GetAllAsync());
    }


    /// <summary>
    /// 递归树
    /// </summary>
    /// <param name="pid">父级Id</param>
    /// <param name="cates">数据源</param>
    /// <returns></returns>
    private async Task<List<ViewMenu>> GetCates(int pid, List<Menu> cates = null)
    {
        var parent = cates.Where(P => P.Pid == pid);
        List<ViewMenu> lists = new();
        foreach (var item in parent)
        {
            ViewMenu Childer = new()
            {
                Id = item.Id,
                Title = item.Name,
                Type = item.Type,
                Icon = item.Icon,
                RoleCode = item.RoleCode,
                Sort = item.Sort,
                Status = item.Status,
                PathUrl = item.PathUrl,
                ComUrl = item.ComUrl,
                Redirect = item.Redirect,
                Hide = item.Hide,
            };
            Childer.Children = await GetSon(Childer, cates);
            lists.Add(Childer);
        }
        async Task<List<ViewMenu>> GetSon(ViewMenu cates, List<Menu> sonCates = null)
        {
            if (!sonCates.Exists(x => x.Pid == cates.Id)) return null;
            else return await GetCates(cates.Id, sonCates);
        }
        return lists.OrderBy(o => o.Sort).ThenBy(o => o.Id).ToList();
    }
    /// <summary>
    /// 根据用户角色输出路由和菜单
    /// </summary>
    /// <param name="user"></param>
    /// <param name="type"></param>
    /// <returns></returns>
    public async Task<List<AsMenu>> GetUserMenu(ILoginUserInfo user, int type)
    {
        if (user.GroupId == 1)
        {
            List<Menu> list = await GetAllAsync();
            return await CascaderCates(0, list.Where(o => o.Type == type)?.ToList());
        }
        else
        {
            return await CascaderCates(0, await CateRoleListAsync(user.SystemRole, type));
        }
    }
    /// <summary>
    /// 根据角色权限输出菜单按钮权限
    /// </summary>
    /// <param name="user"></param>
    /// <param name="type"></param>
    /// <returns></returns>
    public async Task<List<Menu>> GetUserMenuBotton(ILoginUserInfo user, int type)
    {
        return await CateRoleListAsync(user.SystemRole, type);
    }

    /// <summary>
    /// 递归树  级联选择
    /// </summary>
    /// <param name="pid">父级Id</param>
    /// <param name="cates">数据源</param>
    /// <returns></returns>
    private async Task<List<AsMenu>> CascaderCates(int pid, List<Menu> cates = null)
    {
        var parent = cates.Where(P => P.Pid == pid);
        List<AsMenu> lists = new();
        foreach (var item in parent)
        {
            AsMenu Childer = new()
            {
                MenuId = item.Id,
                Id = item.PathUrl,
                Type = item.Type,
                Icon = item.Icon,
                Value = item.Id,
                Title = item.Name,
                ComUrl = item.ComUrl,
                Sort = item.Sort,
                Redirect = item.Redirect,
                Hide = item.Hide,
                RoleCode = item.RoleCode,
            };
            Childer.Children = await GetSon(Childer, cates);
            lists.Add(Childer);
        }
        async Task<List<AsMenu>> GetSon(AsMenu cates, List<Menu> sonCates = null)
        {
            if (!sonCates.Exists(x => x.Pid == cates.Value)) return null;
            else return await CascaderCates(cates.Value, sonCates);
        }
        return lists.OrderBy(o => o.Sort).ThenBy(o => o.Value).ToList();
    }
}
